import { Server as SocketIO } from 'socket.io';
import { setClientSocket } from '../services/webrtc-socket-service.js';

let io, clientSocket;

export function initializeSocket(server) {
  io = new SocketIO(server);

  io.on('connection', (socket) => {
    console.log('Client connected');
    socket.setMaxListeners(20);  // Increase the limit to 20 listeners
    clientSocket = socket;
    setClientSocket(socket);
    socket.on('disconnect', () => {
      console.log('Client disconnected');
    });
   
  });

  return io;
}

export function getSocketIO() {
  if (!io) {
    throw new Error('Socket.io not initialized. Call initializeSocket first.');
  }
  return io;
}


export function getClientSocket() {
  if (!clientSocket) {
    throw new Error('Client socket not initialized. Call initializeSocket first.');
  }
  return clientSocket;
}